package com.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author gaomj
 * @data 2022/12/1 23:32
 */
public class ConJdbc {


    //jdbc
    //设置变量 数据库账户名 密码等

    private static String name="root";
    private static String pssword="mmm18623908829";
    private static String jdbc_driver="com.mysql.cj.jdbc.Driver";

   //url
    private static String url = "jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf-8";

    //加载驱动
    static {

        try{
            //注册驱动
            Class.forName(jdbc_driver);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    //连接
    public static Connection getCon() throws SQLException {
        Connection connection = DriverManager.getConnection(url,name,pssword);

        return connection;

    }


    //关闭
    public void close(Statement statement,Connection connection){
        //编译sql语句的关闭
        if(statement !=null){
            try {
                statement.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        //关闭mysql
        if(connection!=null){

            try {
                connection.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }




    }












}
